// xc_lshop/shop/pack/mycard/com/exchange.js
var app = getApp();

Component({
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
    xcmodel:Object,
    themcolor:Object
  },

  /**
   * 组件的初始数据
   */
  data: {
    show:false,
    allchecked:false,
    allnum:0,//总件数
    choosednum:0,//总商品数
    // item:{}
  },
 
  /**
   * 组件的方法列表
   */
  methods: {
    rechoose(){
      this.setData({
        allchecked:false,
        allnum:0,
        choosednum:0,
        show:false
       })
    },
    init(xcmodel){
 
       this.setData({
        xcmodel:xcmodel,
        allchecked:false,
        allnum:0,
        choosednum:0,
        show:true
       })
    },
    onClose(){
      this.setData({
        show:false
      })
    },
    onChangeItem(e){
      console.log(e)
      var index=e.currentTarget.dataset.index
      var name='xcmodel.proinfo['+index+'].choosed';
      var namenum='xcmodel.proinfo['+index+'].choosednum';

      var choosednum=1;
      var choosedstatus=true;
      console.log(typeof(e.detail));
      // return

      if(typeof(e.detail)=='number'){
        choosednum=e.detail;
        if(choosednum==0){
          choosedstatus=false
        }
      }else if(this.data.xcmodel.proinfo[index].choosednum){
        choosednum=this.data.xcmodel.proinfo[index].choosednum
  
        choosedstatus=!this.data.xcmodel.proinfo[index].choosed
     
      }
      
      this.setData({
        [name]:choosedstatus,
        [namenum]:choosednum
      })
    
      this.changeall()
    },
    changeall(){
      var proinfo=this.data.xcmodel.proinfo
      console.log("proinfo")
      console.log(proinfo)
      var choosednum=0;
      var allnum=0;

      var failnum=0
      for(var i in proinfo){
        if((proinfo[i].allnum - proinfo[i].usednum) > 0){
          if(proinfo[i].choosed){
            choosednum ++
            if(proinfo[i].choosednum){
              allnum +=proinfo[i].choosednum
            }
          }
        }else{
          failnum ++
        }
       
        
      }
      var update={}
      update['allnum']=allnum;
      update['choosednum']=choosednum;

    
      if(choosednum==(proinfo.length - failnum)){
        if(!this.data.allchecked){
          update['allchecked']=true
        }
        
      }else{
       

        if(this.data.allchecked){
          update['allchecked']=false
        }
      }

      this.setData(update)
    },
    onChangeAll(){
      var update={};
      var status=!this.data.allchecked;
      update['allchecked']=status;
      var proinfo=this.data.xcmodel.proinfo
      for(var i in proinfo){
        if((proinfo[i].allnum - proinfo[i].usednum) >0){
          proinfo[i].choosed=status;
          if(status && (!proinfo[i].choosednum)){
            proinfo[i].choosednum=1
          }
        }
        
      }
      update['xcmodel.proinfo']=proinfo;
      this.setData(update)
      this.changeall()
    },
    exchange(){
      console.log("this.data.xcmodel.proinfo")
      console.log(this.data.xcmodel.proinfo)
      var proinfo=this.data.xcmodel.proinfo

      var cart=[]
      for(var i in proinfo){
        if(proinfo[i].choosed){
          var model={
            activityid:this.data.xcmodel.id,
            activityprice:proinfo[i].proinfo.activityprice,
            attrnameinfo:proinfo[i].proinfo.attrnameinfo,
            attrs:[],
            bpid:0,
            cid:-2001,
            clubid: -1,
            flag: -1,
            id:proinfo[i]['id'],
            keyval: proinfo[i].proid+"_-2001_"+this.data.xcmodel.id+"_"+proinfo[i].pro_ident+"_1",
            name: proinfo[i]['name'],
            num: proinfo[i].choosednum,
            price:proinfo[i]['proinfo']['price'],
            pro_ident:proinfo[i].pro_ident,
            proid:proinfo[i]['id'],
            protype: 1,
            realprice:proinfo[i]['proinfo']['price'],
            simg: proinfo[i]['proinfo']['simg'],
            weight: proinfo[i]['proinfo']['weight'],
          };
          console.log("model")
          console.log(model)
          
          cart.push(model)
        }
      
      }
      if(cart.length==0){
        wx.showToast({
          icon:'none',
          title: '请选择兑换的产品',
        })
        return false
      }
      
  
      console.log("cart")
      console.log(cart)
  
      app.globalData.temcart = {};
      app.globalData.temcart["data"] = cart;
      wx.navigateTo({
        url: '/' + app.globalData.xcsys.m + '/shop/paycore/payinfo?iscart=-1&cardid='+this.data.xcmodel.id
      })
    }
  }
})
